package org.robert.export_client.easyexcel.write.impl;


import com.robert.common.commonutil.exception.AppException;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.robert.export_client.aop.ExcelDictAdapter;
import org.robert.export_client.easyexcel.write.ExcelBaseQuery;
import org.robert.export_client.easyexcel.write.IExcelExportService;
import org.robert.export_client.easyexcel.entity.WebRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * ExcelDownLoad:
 *  excel下载器，需要配置EasyExcel的注解使用
 * @author zhaoshengtao
 * @date 2024/01/08
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class ExcelExportSourceService<T> extends WebRequest<T> implements IExcelExportService {

    private ExcelBaseQuery<T> excelBaseQuery;

    private T t;


    public ExcelExportSourceService(HttpServletResponse response, Class<T> clazz, ExcelBaseQuery<T> excelBaseQuery){
        this.clazz = clazz;
        this.response = response;
        this.excelBaseQuery = excelBaseQuery;
    }

    public ExcelExportSourceService(HttpServletResponse response, Class<T> clazz,
                                    ExcelBaseQuery<T> excelBaseQuery, Map<String, Map<String, String>> dictMap){
        this.response = response;
        this.clazz = clazz;
        this.excelBaseQuery = excelBaseQuery;
        this.dictMap = dictMap;
        if(org.apache.commons.collections4.MapUtils.isNotEmpty(dictMap)){
            this.checker = new ExcelDictAdapter<T>();
        }
    }

    public ExcelExportSourceService(HttpServletResponse response, Class<T> clazz,
                                    ExcelBaseQuery<T> excelBaseQuery, Map<String,
            Map<String, String>> dictMap, String fileName){
        this.response = response;
        this.clazz = clazz;
        this.excelBaseQuery = excelBaseQuery;
        this.dictMap = dictMap;
        this.fileName = fileName;
        if(org.apache.commons.collections4.MapUtils.isNotEmpty(dictMap)){
            this.checker = new ExcelDictAdapter<T>();
        }
    }

    public ExcelExportSourceService(HttpServletResponse response, Class<T> clazz,
                                    ExcelBaseQuery<T> excelBaseQuery, Map<String,
            Map<String, String>> dictMap, String fileName, String sheetName){
        this.response = response;
        this.clazz = clazz;
        this.excelBaseQuery = excelBaseQuery;
        this.dictMap = dictMap;
        this.fileName = fileName;
        this.sheetName = sheetName;
        if(org.apache.commons.collections4.MapUtils.isNotEmpty(dictMap)){
            this.checker = new ExcelDictAdapter<T>();
        }
    }

    @Override
    public void exportToExcel() throws Exception {
        if(!check() || excelBaseQuery == null){
            throw new AppException("500", "参数异常");
        }
        List<T> list = excelBaseQuery.query(t);
        ExcelExportDataService<T> service = new ExcelExportDataService<>(response, list, clazz,
                dictMap, fileName, sheetName);
        service.exportToExcel();
    }
}
